<?php
session_start();
if(isset($_POST['submit']))
{
 foreach($_POST['qty'] as $key=>$value)
 {
  if( ($value == 0) and (is_numeric($value)))
  {
   unset ($_SESSION['DT'][$key]);
  }
  elseif(($value > 0) and (is_numeric($value)))
  {
   $_SESSION['DT'][$key]=$value;
  }
 }
 header("location:DT.php");
}
?>
<html>
<head>
<?php $c = "active"; ?>
<?php
include ('head.php');
?>
<script language="javascript">
	
	function CheckSL(SL,SLTK,n)
	{
		SL = '0' + SL;
		SLTK = '0' + SLTK;
		SL = parseFloat(SL);
		SLTK = parseFloat(SLTK);
		
			if(SL > SLTK)
			{
				alert("Số lượng hàng tồn không đủ !!!");
				var txt1  = document.getElementById(n);
				txt1.value = SLTK;
				
			}
	}


</script>

</head>

<body>
		
<?php
include ('dbcon.php');
include ('webtitle.php');
include ('topbannermenu.php');
include ('leftmenu.php');
include ('rightmenu.php');

?>	
   
  <div id = "divMid">
      <div id = "pro4">
        <center>
<font color="#0000CC"> <br><h1>GIỎ HÀNG CỦA BẠN</h1></font>
<?php

$ok=1;
if(isset($_SESSION['DT']))
{
 foreach($_SESSION['DT'] as $k => $v)
 {
  if(isset($k))
  {
   $ok=2;
  }
 }
}
if($ok == 2)
{

   echo "<form action='DT.php' method='post'>";
   foreach($_SESSION['DT'] as $key=>$value)
   {
    $item[]=$key;
   }
   $str=implode(",",$item);
   $connect=mysql_connect("localhost","root","") or die("Can not connect database");
   mysql_select_db("dtdd",$connect);
   mysql_query("SET NAMES 'utf8'", $connect);
   $query=mysql_query("select * from sanpham where MaSP in ($str)");
   $total = "";
   $id = "";
   while($row=mysql_fetch_array($query))
   {
	   $id  = $row[0];
   echo "<div class='pro'>";
   echo "<h3>$row[1]</h3><br />";
   echo "<img src ='$row[4]' width='130px' height='160px'<br /><br /><br />";
   echo "<p align='right'><h4>Đơn Giá: ". number_format($row[3],0) ." VND</h4></p><br />";
   
   echo "<p align='right'><h4>Số Lượng: <input type='text' name='qty[$id]' id='qty[$id]'  size='5' value='{$_SESSION['DT'][$id]}' onKeyUp = 'CheckSL(this.value,$row[6],this.id);' > - ";
   echo "<a href='delDT.php?productid=$row[0]'>Xóa món hàng</a></h4></p><br />";
   echo "<p align='right'> <h4>Giá tiền cho món hàng: ". number_format($_SESSION['DT'][$row[0]]*$row[3],0)." VND</h4></p>";
   echo "</div>";
   $total+=$_SESSION['DT'][$row[0]]*$row[3];
   }
  echo "<div class='pro' align='right'>";
  echo "<b><h3>Tổng tiền thanh toán : <font color='red'>". number_format($total,0)." VND</font></h3></b>";
  echo "</div>";
  echo "<input align='left' type='submit' name='submit'  class = 'css'value=' Cập nhật giỏ hàng '/> </font>";
  echo "<div class='pro' align='center'>";
  echo "<b><a href='index.php'>Tiếp tục chọn hàng</a> - <a href='delDT.php?productid=0'>Xóa bỏ giỏ hàng</a></b>";
  echo "</div>"; 
  echo "</form>";
  echo '<form action="thanhtoan.php" method = "post">';
  echo "<input type = 'submit' name = 'thanhtoan' class = 'css' value = 'Thanh Toán'>";
  echo "</form>";
}?>
</center>
</div>
</div>

		<div class="clr"></div>
		<?php 
		include ('footer.php');
		?>
</body>
</html>

